﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;

namespace MmCat.Inc
{
    /// <summary>
    /// Up 的摘要说明
    /// </summary>
    public class Up : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string _refilepath = context.Request.QueryString["ReFilePath"]; //取得返回的对象名称
            string _upfilepath = context.Request.QueryString["UpFilePath"]; //取得上传的对象名称

            HttpPostedFile _upfile = context.Request.Files[_upfilepath];
            string _delfile = context.Request.Params[_refilepath];

            if (_upfile == null)
            {
                context.Response.Write("{msg: 0, msbox: \"请选择要上传文件！\"}");
                return;
            }
            string msg = fileSaveAs(_upfile);
            //删除已存在的旧文件
            if (!string.IsNullOrEmpty(_delfile) && msg.IndexOf("UploadPic") >= 0)
            {
                string _filename = context.Server.MapPath("/") + "/" + _delfile;
                if (File.Exists(_filename))
                {
                    File.Delete(_filename);
                }
            }
            //返回成功信息
            context.Response.Write(msg);
        }


        ///<summary>
        /// 文件上传方法
        /// </summary>
        public string fileSaveAs(HttpPostedFile _postedFile)
        {
            try
            {
                string _fileExt = _postedFile.FileName.Substring(_postedFile.FileName.LastIndexOf(".") + 1);
                //验证合法的文件
                if (!CheckFileExt("GIF|JPG|PNG", _fileExt))
                {
                    return "不允许上传" + _fileExt + "类型的文件！";
                }
                if (_postedFile.ContentLength > 200 * 1024)
                {
                    return "文件超过限制(200K)的大小啦！";
                }
                string _fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + _fileExt; //随机文件名
                //获得要保存的文件路径
                string path = Path.GetDirectoryName(Path.GetDirectoryName(HttpContext.Current.Server.MapPath("/")));
                string foldName = path + "/Print/UploadPic/" + DateTime.Now.ToString("yyyyMM");// HttpContext.Current.Server.MapPath("/UploadPic/" + DateTime.Now.ToString("yyyyMM"));
                System.Diagnostics.Debug.WriteLine(foldName);
                if (!Directory.Exists(foldName))
                {
                    Directory.CreateDirectory(foldName);
                }
                string serverFileName = foldName + "/" + _fileName;
                
                //保存文件
                _postedFile.SaveAs(serverFileName);


                return "/UploadPic/" + DateTime.Now.ToString("yyyyMM") + "/" + _fileName;
            }
            catch
            {
                return "上传过程中发生意外错误！";
            }
        }

        /// <summary>
        /// 检查是否为合法的上传文件
        /// </summary>
        /// <returns></returns>
        private bool CheckFileExt(string _fileType, string _fileExt)
        {
            string[] allowExt = _fileType.Split('|');
            for (int i = 0; i < allowExt.Length; i++)
            {
                if (allowExt[i].ToLower() == _fileExt.ToLower()) { return true; }
            }
            return false;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}